import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JTextField;
import javax.swing.JButton;


public class EditUserPanel extends JPanel {
	
	JTextField txtName;
	
	JTextField txtAddress;
	
	JTextField txtContactPhone;
	
	JTextField txtEmailAddress;
	
	JTextField txtUsername;
	
	JTextField txtPassword;
	
	private JLabel lblInstructions;
	private JButton btnLoadUser;
	private JLabel lblResult;
	private JButton btnSaveUser;
	private User current;
	
	private class buttonListen implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if(arg0.getSource().equals(btnLoadUser)){
				ArrayList<User> result = UserDatabase.searchUser(
						txtUsername.getText(), UserDatabase.USERNAME_SEARCH);
				if(!result.isEmpty()){
					current = result.get(0);
					txtName.setText(current.name);
					txtAddress.setText(current.address);
					txtContactPhone.setText(current.phoneNum);
					txtEmailAddress.setText(current.email);
					txtPassword.setText(current.password);
					lblResult.setText("User Loaded");
				}
				else{
					lblResult.setText("Username not found");
				}
			}
			else if(arg0.getSource().equals(btnSaveUser)){
				User saved = current;
				saved.setName(txtName.getText());
				saved.setAddress(txtAddress.getText());
				saved.setPhoneNum(txtContactPhone.getText());
				saved.setEmail(txtEmailAddress.getText());
				saved.setPassword(txtPassword.getText());
				
				UserDatabase.removeUser(current.username, UserDatabase.DOCTOR);
				UserDatabase.removeUser(current.username, UserDatabase.PATIENT);
				UserDatabase.removeUser(current.username, UserDatabase.SYSTEM_ADMIN);
				UserDatabase.removeUser(current.username, UserDatabase.NURSE);
				
				if(saved.usertype.equals("doctor")){
					UserDatabase.addUser(saved, UserDatabase.DOCTOR);
				}
				else if(saved.usertype.equals("patient")){
					UserDatabase.addUser(saved, UserDatabase.PATIENT);
				}
				else if(saved.usertype.equals("nurse")){
					UserDatabase.addUser(saved, UserDatabase.NURSE);
				}
				else if(saved.usertype.equals("admin")){
					UserDatabase.addUser(saved, UserDatabase.SYSTEM_ADMIN);
				}
				
				DataSaveLoad.save();
				
				lblResult.setText("User Saved");
			}
		}

		
	}
	public EditUserPanel() {
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);
		
		txtName = new JTextField();
		springLayout.putConstraint(SpringLayout.WEST, txtName, 85, SpringLayout.WEST, this);
		add(txtName);
		txtName.setColumns(10);
		
		txtAddress = new JTextField();
		springLayout.putConstraint(SpringLayout.SOUTH, txtName, -18, SpringLayout.NORTH, txtAddress);
		springLayout.putConstraint(SpringLayout.NORTH, txtAddress, 153, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, txtAddress, 85, SpringLayout.WEST, this);
		add(txtAddress);
		txtAddress.setColumns(10);
		
		txtContactPhone = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, 
				txtContactPhone, 19, SpringLayout.SOUTH, txtAddress);
		springLayout.putConstraint(SpringLayout.WEST, txtContactPhone, 85, SpringLayout.WEST, this);
		add(txtContactPhone);
		txtContactPhone.setColumns(10);
		
		txtEmailAddress = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, 
				txtEmailAddress, 18, SpringLayout.SOUTH, txtContactPhone);
		springLayout.putConstraint(SpringLayout.WEST, txtEmailAddress, 85, SpringLayout.WEST, this);
		add(txtEmailAddress);
		txtEmailAddress.setColumns(10);
		
		txtUsername = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, txtUsername, 0, SpringLayout.NORTH, txtName);
		springLayout.putConstraint(SpringLayout.EAST, txtUsername, -78, SpringLayout.EAST, this);
		add(txtUsername);
		txtUsername.setColumns(10);
		
		txtPassword = new JTextField();
		springLayout.putConstraint(SpringLayout.NORTH, txtPassword, 0, SpringLayout.NORTH, txtAddress);
		springLayout.putConstraint(SpringLayout.EAST, txtPassword, -78, SpringLayout.EAST, this);
		add(txtPassword);
		txtPassword.setColumns(10);
		
		btnLoadUser = new JButton("Load User");
		springLayout.putConstraint(SpringLayout.NORTH, btnLoadUser, -1, SpringLayout.NORTH, txtContactPhone);
		springLayout.putConstraint(SpringLayout.EAST, btnLoadUser, 0, SpringLayout.EAST, txtUsername);
		add(btnLoadUser);
		btnLoadUser.addActionListener(new buttonListen());
		
		JLabel lblAddANew = new JLabel("Edit User");
		springLayout.putConstraint(SpringLayout.WEST, 
				lblAddANew, 132, SpringLayout.WEST, this);
		lblAddANew.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.NORTH, 
				lblAddANew, 22, SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.EAST, lblAddANew, 308, SpringLayout.WEST, this);
		lblAddANew.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
		add(lblAddANew);
		
		lblInstructions = 
			new JLabel("Type in Username, then hit load.");
		lblInstructions.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.NORTH, txtName, 33, SpringLayout.SOUTH, lblInstructions);
		springLayout.putConstraint(SpringLayout.NORTH, lblInstructions, 24, SpringLayout.SOUTH, lblAddANew);
		springLayout.putConstraint(SpringLayout.SOUTH, lblInstructions, 40, SpringLayout.SOUTH, lblAddANew);
		springLayout.putConstraint(SpringLayout.WEST, lblInstructions, 57, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblInstructions, 372, SpringLayout.WEST, this);
		add(lblInstructions);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setFont(new Font("Tahoma", Font.BOLD, 11));
		springLayout.putConstraint(SpringLayout.NORTH, lblName, 3, SpringLayout.NORTH, txtName);
		springLayout.putConstraint(SpringLayout.WEST, lblName, 27, SpringLayout.WEST, this);
		add(lblName);
		
		JLabel lblAddress = new JLabel("Address:");
		springLayout.putConstraint(SpringLayout.NORTH, lblAddress, 3, SpringLayout.NORTH, txtAddress);
		springLayout.putConstraint(SpringLayout.EAST, lblAddress, 0, SpringLayout.EAST, lblName);
		lblAddress.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblAddress);
		
		JLabel lblPhone = new JLabel("Phone #:");
		springLayout.putConstraint(SpringLayout.NORTH, lblPhone, 3, SpringLayout.NORTH, txtContactPhone);
		springLayout.putConstraint(SpringLayout.WEST, lblPhone, 0, SpringLayout.WEST, lblAddress);
		lblPhone.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblPhone);
		
		JLabel lblNewLabel = new JLabel("E-Mail:");
		springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 3, SpringLayout.NORTH, txtEmailAddress);
		springLayout.putConstraint(SpringLayout.EAST, lblNewLabel, 0, SpringLayout.EAST, lblName);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblNewLabel);
		
		JLabel lblUsername = new JLabel("Username:");
		springLayout.putConstraint(SpringLayout.WEST, btnLoadUser, 0, SpringLayout.WEST, lblUsername);
		springLayout.putConstraint(SpringLayout.WEST, lblUsername, 37, SpringLayout.EAST, txtName);
		springLayout.putConstraint(SpringLayout.WEST, txtUsername, 17, SpringLayout.EAST, lblUsername);
		springLayout.putConstraint(SpringLayout.NORTH, lblUsername, 3, SpringLayout.NORTH, txtName);
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password:");
		springLayout.putConstraint(SpringLayout.WEST, lblPassword, 37, SpringLayout.EAST, txtAddress);
		springLayout.putConstraint(SpringLayout.WEST, txtPassword, 21, SpringLayout.EAST, lblPassword);
		springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 3, SpringLayout.NORTH, txtAddress);
		lblPassword.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblPassword);
		
		String[] options = { "Patient", "Nurse", "Doctor", "Admin"};
		
		lblResult = new JLabel("");
		springLayout.putConstraint(SpringLayout.WEST, lblResult, 149, SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, lblResult, -137, SpringLayout.EAST, this);
		lblResult.setHorizontalAlignment(SwingConstants.CENTER);
		springLayout.putConstraint(SpringLayout.SOUTH, lblResult, -10, SpringLayout.SOUTH, this);
		lblResult.setForeground(Color.RED);
		lblResult.setFont(new Font("Times New Roman", Font.ITALIC, 13));
		add(lblResult);
		
		btnSaveUser = new JButton("Save User");
		springLayout.putConstraint(SpringLayout.NORTH, btnSaveUser, 13, SpringLayout.SOUTH, btnLoadUser);
		springLayout.putConstraint(SpringLayout.WEST, btnSaveUser, 0, SpringLayout.WEST, btnLoadUser);
		springLayout.putConstraint(SpringLayout.EAST, btnSaveUser, 0, SpringLayout.EAST, txtUsername);
		add(btnSaveUser);
		btnSaveUser.addActionListener(new buttonListen());
	}
	
	public JTextField getTxtUsername() {
		return txtUsername;
	}
	
	public JTextField getTxtAddress() {
		return txtAddress;
	}
	
	public JButton getBtnCreateUser() {
		return btnLoadUser;
	}
	
	public JTextField getTxtContactPhone() {
		return txtContactPhone;
	}
	
	public JTextField getTxtEmailAddress() {
		return txtEmailAddress;
	}
	
	public JTextField getTxtPassword() {
		return txtPassword;
	}
	
	public JTextField getTxtName(){
		return txtName;
	}
	
	public JLabel getLblResult(){
		return lblResult;
	}
	
	public JButton getBtnSaveUser(){
		return btnSaveUser;
	}
	
	public String toString(){
		return "editUser";
	}
}
